﻿#include "addcommodity.h"
#include "ui_addcommodity.h"

AddCommodity::AddCommodity(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AddCommodity)
{
    ui->setupUi(this);

    //禁止窗口最大化按钮
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(),this->height());
    ui->IdlineEdit->setFocus();
}

AddCommodity::~AddCommodity()
{
    delete ui;
}

void AddCommodity::on_exitButton_clicked()
{
    close();
}


void AddCommodity::on_addButton_clicked()
{
    QSqlQuery sql;
    QString strId,strName,strAmount,strUnitPrice,strSupplier,strDirector,strInputTime,strOutputTime,strRemarks;
    strId=ui->IdlineEdit->text();
    strName=ui->NamelineEdit->text();
    strAmount=ui->AmountlineEdit->text();
    strUnitPrice=ui->UnitPricelineEdit->text();
    strSupplier=ui->SupplierlineEdit->text();
    strDirector=ui->DirectorlineEdit->text();

    QDateTime datatime=QDateTime::currentDateTime();
    QString strnowinputtime=datatime.toString("yyyy-MM-dd HH:mm:ss");

    strInputTime=strnowinputtime;
    strOutputTime=strnowinputtime;

    strRemarks=ui->RemarkstextEdit->toPlainText();  

    //QMessageBox::information(this,"测试",strId+strName+strRemarks);

    if(ui->IdlineEdit->text().isEmpty())
    {
        QMessageBox::critical(this,"警告","新增商品编号不能为空，请重新输入！");
        ui->IdlineEdit->setFocus();

        return;
    }
    QString sqlquery=QString("insert into commoditydatatable(StockId,StockName,StockAmount,StockUnitprice,Supplier,Director,WareHousetime,DeliveryTime,Remarks) "
                               "values('%1','%2','%3','%4','%5','%6','%7','%8','%9')")
                                .arg(strId,strName,strAmount,strUnitPrice,strSupplier,strDirector,strInputTime,strOutputTime,strRemarks);
    if(sql.exec(sqlquery))
    {
        QMessageBox::information(this,"提示","插入成功！");
    }
    else
    {
        QMessageBox::critical(this,"错误", "插入失败！错误信息：" + sql.lastError().text());
    }

}

